package com.shared.controller;

import com.shared.dao.PGoodsCartMapper;
import com.shared.dao.PGoodsMapper;
import com.shared.pojo.PGoods;
import com.shared.pojo.PGoodsCart;
import com.shared.server.PGoodsCartServer;
import com.shared.util.RestDTO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class PGoodsCartController {

    @Autowired
    private PGoodsCartServer pGoodsCartServer;
    @Autowired
    private PGoodsCartMapper pGoodsCartMapper;
    @Autowired
    private PGoodsMapper pGoodsMapper;

    @GetMapping("/cart/{userid}")
    @ResponseBody
    @ApiOperation(value = "根据用户id查询购物车信息")
    public List<PGoodsCart> findCartByUserId(@PathVariable("userid") Integer userid) {
        return pGoodsCartServer.findCartByUserId(userid);
    }

    @DeleteMapping("/deletecart/{id}")
    @ResponseBody
    @ApiOperation(value = "根据id删除购物车信息")
    public void deleteCart(@PathVariable("id") Integer id) {
        PGoodsCart pGoodsCart = pGoodsCartMapper.queryById(id);
        this.pGoodsCartServer.deleteCartById(id);
        this.pGoodsMapper.updateCartNumberById(pGoodsCart.getGoodsid(), +pGoodsCart.getNumber());

    }

    @PostMapping(value = "/addcart/{id}")
    @ResponseBody
    @ApiOperation(value = "增加购物车信息")
    public RestDTO addCart(@PathVariable("id") Integer id) {
        List<PGoods> list = pGoodsMapper.findGoodsByid(id);
        PGoodsCart pGoodsCart = new PGoodsCart();
        for (PGoods pGoods : list) {
            pGoodsCart.setGoodsid(pGoods.getId());
            pGoodsCart.setUserid(1);
            pGoodsCart.setImg(pGoods.getPic());
            pGoodsCart.setName(pGoods.getTitle());
            pGoodsCart.setPrice(pGoods.getPrice());
            pGoodsCart.setNumber(1);
        }
        PGoodsCart pGoodsCart1 = pGoodsCartMapper.findCartByGoodsId(id);
        if (pGoodsCart1 == null) {
            this.pGoodsCartServer.addCart(pGoodsCart);
            this.pGoodsMapper.updateCartNumberById(pGoodsCart.getGoodsid(), -1);
            return RestDTO.ADD_SUCCESS;
        } else {
            return RestDTO.ADD_FAILURE;
        }

    }

    @PostMapping("/subcartnum/{id}")
    @ResponseBody
    @ApiOperation(value = "根据用户id减少购物车商品数量")
    public RestDTO cartnum(@PathVariable("id") Integer id) {
        PGoodsCart pGoodsCart = pGoodsCartMapper.queryById(id);
        PGoods pGoods = pGoodsMapper.queryById(pGoodsCart.getGoodsid());
        if (pGoodsCart.getNumber() > 1) {
            this.pGoodsCartServer.updateCartNumberById(id, -1);
            this.pGoodsMapper.updateCartNumberById(pGoods.getId(), 1);
            return RestDTO.ADD_SUCCESS;
        }
        return RestDTO.ADD_FAILURE;
    }

    @PostMapping("/addcartnum/{id}")
    @ResponseBody
    @ApiOperation(value = "根据用户id增加购物车商品数量")
    public RestDTO addnum(@PathVariable("id") Integer id) {
        PGoodsCart pGoodsCart = pGoodsCartMapper.queryById(id);
        PGoods pGoods = pGoodsMapper.queryById(pGoodsCart.getGoodsid());
        if (pGoodsCart.getNumber() >= 5) {
            return RestDTO.Cat_MAX;
        }
        if (pGoods.getStock() > 0) {
            this.pGoodsCartServer.updateCartNumberById(id, 1);
            this.pGoodsMapper.updateCartNumberById(pGoods.getId(), -1);
            return RestDTO.ADD_SUCCESS;
        }
        return RestDTO.ADD_FAILURE;
    }
}
